@extends('layouts.app')

@section('content')
    <div class="card">
        <div class="card-header">接口列表</div>

        <div class="card-body">
{{--            search--}}
            <form class="form-inline">
                <label class="sr-only" for="inlineFormInputName2">Name</label>
                <input type="text" class="form-control mb-2 mr-sm-2" name="search_name" value="{{ request()->search_name }}" id="inlineFormInputName2" placeholder="按接口名称搜索">

                <label class="sr-only" for="inlineFormInputGroupUsername2">分类</label>
                <div class="input-group mb-2 mr-sm-2">
                    <div class="input-group-prepend">
                        <div class="input-group-text">分类</div>
                    </div>
                    <select name="type_id" id="search-type" class="form-control" >
                        <option value="">=请选择=</option>
                        @foreach($types as $type)
                            <option value="{{ $type->id }}" @if(request()->type_id == $type->id) selected @endif>{{ $type->title }}</option>
                        @endforeach
                    </select>
                </div>


                <button type="submit" class="btn btn-primary mb-2">搜索</button>
            </form>

            <table class="table table-bordered table-hover">
                <thead>
                <tr>
                    <th scope="col">ID</th>
                    <th scope="col">分类</th>
                    <th scope="col">名称</th>
                    <th scope="col">操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach($articles as $article)
                    <tr>
                        <td>{{ $article->id }}</td>
                        <td>{{ $article->type->title }}</td>
                        <td>{{ $article->name }}</td>
                        <td>
                            <a class="btn btn-sm btn-primary" href="{{ route('articles.edit', [$article]) }}" role="button">编辑</a>
                            <a class="btn btn-sm btn-info" href="{{ route('articles.show', [$article]) }}" role="button">查看</a>
                            <button class="btn btn-sm btn-danger" role="button" onclick="delData({{ $article->id }}, '{{ $article->name }}')">删除</button>
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            <div class="float-right">{{ $articles->appends(['search_name' => request()->search_name, 'type_id' => request()->type_id])->links() }}</div>
        </div>
    </div>
@endsection

@push('js')
    <script>
        $("#list-article").addClass('active');
        function delData(id, title){
            layer.confirm("确认要删除接口: " + title + " 吗?", {title: "删除接口数据"}, function(){
                let url = '/articles/' + id;
                console.log(url);
                $.ajax({
                    url: url,
                    type: 'DELETE',
                    data: {
                        '_token': token,
                    },
                    success:function (data) {
                        if (data.code === 200) {
                            layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                                window.location.reload();
                            });
                        }else{
                            layer.msg(data.msg || "操作失败");
                        }
                    },
                    error:function(){
                        layer.msg("系统错误，请联系管理员");
                    }
                });
            });
        }
    </script>
@endpush

